#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/30 12:52
from domain.common.schema_item import SchemaItem
from domain.report.common.period_enum import PeriodEnum


class BaseSchema:
    _schemas = []

    _name2schema = {}

    _title2name = {}

    @classmethod
    def _init_name2schema(cls):
        if cls._schemas is not None and len(cls._schemas) > 0:
            for schema in cls._schemas:
                schema_list = cls._name2schema.get(schema.name, list())
                schema_list = schema_list if schema_list is not None or len(schema_list) > 0 else list()
                schema_list.append(schema)
                cls._name2schema[schema.name] = schema_list

    @classmethod
    def _init_title2name(cls):
        if cls._schemas is not None and len(cls._schemas) > 0:
            for schema in cls._schemas:
                cls._title2name[schema.title] = schema

    @classmethod
    def get_schema(cls, name: str):
        if cls._name2schema is None or len(cls._name2schema) == 0:
            cls._init_name2schema()
        result_list = cls._name2schema.get(name, None)
        if result_list is not None and len(result_list) > 0:
            return result_list[0]
        return None

    @classmethod
    def get_name_from_title(cls, title: str):
        if cls._title2name is None or len(cls._title2name) == 0:
            cls._init_title2name()
        result_list = cls._title2name.get(title, None)
        if result_list is not None and len(result_list) > 0:
            return result_list[0]
        return None

    @classmethod
    def set(cls, dto, name: str, value):
        if name is None:
            return False
        value = value.replace("-", "") if value and "-" in value else value
        value = value.replace("(", "") if value and "(" in value else value
        value = value.replace(")", "") if value and ")" in value else value
        schemaItem: SchemaItem = cls.get_schema(name)
        if schemaItem:
            if isinstance(value, schemaItem.value_type):
                dto.set(name, value)
                return True
            elif schemaItem.value_type == str:
                value = "" if value is None else str(value)
                dto.set(name, value)
                return True
            elif schemaItem.value_type == float:
                value = value.replace(",", "") if "," in value else value
                value = 0 if value is None or value == "" else float(value)
                dto.set(name, value)
                return True
            elif schemaItem.value_type == int:
                value = value.replace(",", "") if "," in value else value
                value = 0 if value is None or value == "" else int(value)
                dto.set(name, value)
                return True
            elif schemaItem.value_type == PeriodEnum:
                value = int(value)
                if value in PeriodEnum.get_values():
                    dto.set(name, value)
                return True
        return False

    @classmethod
    def set_by_title(cls, dto, title:str, value):
        return cls.set(dto, cls.get_name_from_title(title), value)

    @classmethod
    def get_all_titles(cls):
        return [schema.title for schema in cls._schemas]
